package com.itheima.controller;

import com.itheima.health.entity.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
//web 全局异常处理机制
//业务一旦出现异常，该对象就会捕捉到，封装到Result里面去
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler
    public Result exceptionHandle(Exception ex){
        ex.printStackTrace();
        //获取异常信息
        String errorMsg = ex.getMessage();
        if(errorMsg.length() > 200)
            return new Result(false,errorMsg.substring(0,200) + "...");
        else
            return new Result(false,errorMsg);
    }

}
